package cmw.com.beijingnew;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.Toast;

import cmw.com.beijingnew.activity.GuideActivity;
import cmw.com.beijingnew.activity.MainActivity;
import cmw.com.utils.CacheUtil;

public class SplashActivity extends Activity {
    public static final String START_MAIN = "startMain";
    private RelativeLayout rl_splash_root;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        rl_splash_root = (RelativeLayout) findViewById(R.id.rl_splash_root);

        /**-- step 1 : 渐变动画 --**/
        AlphaAnimation aa = new AlphaAnimation(0,1);
        aa.setDuration(500);
        aa.setFillAfter(false);

        /**-- step 2 : 缩放动画 --**/
        ScaleAnimation sa = new ScaleAnimation(0,1,0,1,ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(500);
        sa.setFillAfter(false);

        /**-- step 3 : 旋转动画 --**/
        RotateAnimation ra = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        ra.setDuration(500);
        ra.setFillAfter(false);

        /**-- 添加的三个动画没有先后顺序，便于同时播放 --**/
        AnimationSet set = new AnimationSet(false);
        set.addAnimation(aa);
        set.addAnimation(sa);
        set.addAnimation(ra);
        set.setDuration(2000);
        set.setAnimationListener(new MyAnimationListner());
        rl_splash_root.startAnimation(set);

    }

    class MyAnimationListner implements Animation.AnimationListener{

        @Override

        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //判断是否进入过主页面
            boolean isStartMain = CacheUtil.getBoolean(SplashActivity.this,START_MAIN);
            Intent intent = null;
            if(isStartMain){/*进入主页面*/
                 intent = new Intent(SplashActivity.this, MainActivity.class);
            }else{/*进入引导页面*/
                 intent = new Intent(SplashActivity.this, GuideActivity.class);
            }
            startActivity(intent);
            finish();
            Toast.makeText(SplashActivity.this, "动画播放完成...",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}
